#!groovy

@Library('jenkinslib') _

def tools = new org.devops.tools()
def ansible = new org.devops.ansible()

pipeline {
    agent {
        node {
            label 'built-in'
        }
    }

    options {
        timestamps()
        skipDefaultCheckout()
        disableConcurrentBuilds()
        timeout(time: 1, unit: 'HOURS')
    }

    stages {
        stage('OperationType') {
            parallel {
                stage('GetCode') {
                    when {
                        environment name: 'Status', value: 'Deploy'
                    }
                    steps {
                        timeout(time: 5, unit: 'MINUTES') {
                            script {
                                echo"获取代码"
                                tools.PrintMes("获取代码-pringMes",'green')
                                checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], extensions: [], userRemoteConfigs: [[ credentialsId: 'e5725766-2dea-4390-947c-d78a97983b3e',url: "${srcUrl}"]]])
                            }
                        }
                    }
                }

                stage('RollBack') {
                    when {
                        environment name: 'Status', value: 'RollBack'
                    }
                    steps {
                        timeout(time: 1, unit: 'MINUTES') {
                            script {
                                echo"回滚开始"
                                //tools.PrintMes("回滚开始",'green')
                                //backup.PhpRollback("${JENKINS_HOME}","${WORKSPACE}","${JOB_NAME}","${Version}")
                            }
                        }
                    }
                }
            }
        }



        stage('deploy'){
            steps{
                timeout(time: 20, unit: 'MINUTES'){
                    script{
                        echo"部署服务-主机列表"
                        //去除前后空格，换行符等
                        def hostss = ansible.AnsibleList("${serverIp}").trim()
                        //以空格分割主机列表并定义字符串数组
                        String[] hostList=hostss.split(' ');
                        for(host in hostList)
                        {
                            hostl = "${host}"
                            echo"${host}"

                            switch(hostl){
                                case "211.159.173.70":

                                    //sh " ansible ${host} -m copy -a 'src=${WORKSPACE} dest=/www/wwwroot/laravel_layui' mode=777 "
                                    sh "rsync -avz ${WORKSPACE}/* ${home}"  
                                    sh "chmod -R 777 /www/wwwroot/pre-jiyun.morelikely.cn" 
                                    tools.PrintMes("该地址的站点发布成功并通过状态码检测",'green')
                                    break;
                        
                                default:
                                    tools.PrintMes("没有找到部署机器",'red')
                                    error "部署失败"
                                    break;
                            }
                   
                        }
                    }
                }
            }
        }
    }


    post {
        success{
            echo"发版成功"
            script{
                echo"发布成功"
                //dingding.notice("${branchName}","构建成功✅",notifypeople)
            }
        }
        failure{
            echo"发版失败"
            script{
                echo"发布失败"
                //dingding.notice("$branchName","构建失败❌",notifypeople)
            }
        }
    }


}












